package com.futuremind.recyclerviewfastscroll.example.customview;


import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

import com.futuremind.recyclerviewfastscroll.ListContainerScrolledListener;
import com.futuremind.recyclerviewfastscroll.Utils;
import com.futuremind.recyclerviewfastscroll.viewprovider.DefaultBubbleBehavior;
import com.futuremind.recyclerviewfastscroll.viewprovider.ScrollerViewProvider;
import com.futuremind.recyclerviewfastscroll.viewprovider.ViewBehavior;
import com.futuremind.recyclerviewfastscroll.viewprovider.VisibilityAnimationManager;

/**
 * Created by Michal on 05/08/16.
 */
public class CustomScrollerViewProvider extends ScrollerViewProvider {
    private Text bubble;
    private Component handle;

    @Override
    public Component provideHandleView(ComponentContainer container) {
        handle = new Component(getContext());
        int dimen = AttrHelper.vp2px(30, getContext());
        handle.setLayoutConfig(new ComponentContainer.LayoutConfig(dimen, dimen));
        Utils.setBackground(handle, drawCircle(dimen, dimen, Color.getIntColor("#90a4ae")));
        handle.setVisibility(Component.INVISIBLE);
        return handle;
    }

    @Override
    public Component provideBubbleView(ComponentContainer container) {
        bubble = new Text(getContext());
        int dimen = AttrHelper.vp2px(60, getContext());
        bubble.setLayoutConfig(new ComponentContainer.LayoutConfig(dimen, dimen));
        Utils.setBackground(bubble, drawCircle(dimen, dimen, Color.getIntColor("#607d8b")));
        bubble.setVisibility(Component.INVISIBLE);
        bubble.setTextAlignment(TextAlignment.CENTER);
        bubble.setTextSize(14, Text.TextSizeType.FP);
        bubble.setTextColor(new Color(Color.getIntColor("#e0e0e0")));
        getScroller().addScrolledListener(new ListContainerScrolledListener.ScrolledListener() {
            @Override
            public void onScroll(float relativePos) {
                //Yeah, yeah, but we were so preoccupied with whether or not we could,
                //that we didn't stop to think if we should.
                bubble.setRotation(relativePos * 360f);
            }
        });
        return bubble;
    }

    @Override
    public Text provideBubbleTextView() {
        return bubble;
    }

    @Override
    public float getBubbleOffset() {
        return  getScroller().isVertical() ?  handle.getHeight() / 2 -  bubble.getHeight() / 2 :  handle.getWidth() / 2 - bubble.getWidth() / 2;
    }

    @Override
    protected ViewBehavior provideHandleBehavior() {
        return new CustomHandleBehavior(
            new VisibilityAnimationManager.Builder(handle)
                .withHideDelay(2000)
                .build(),

            new CustomHandleBehavior.HandleAnimationManager.Builder(handle)
                .withGrabAnimator(new AnimatorProperty()
                    .scaleX(1.5f).scaleY(1.5f).alphaFrom(1.0f).alpha(0.5f))
                .withReleaseAnimator(new AnimatorProperty().
                    scaleX(1.0f).scaleY(1.0f).alphaFrom(0.5f).alpha(1.0f))
                .build()
        );
    }

    @Override
    protected ViewBehavior provideBubbleBehavior() {
        return new DefaultBubbleBehavior(new VisibilityAnimationManager.Builder(bubble).withHideDelay(0).build());
    }

    private static ShapeElement drawCircle(int width, int height, int color) {
        ShapeElement oval = new ShapeElement();
        oval.setShape(ShapeElement.OVAL);
        oval.setRgbColor(RgbColor.fromArgbInt(color));
        return oval;
    }
}
